<template>
  <n-modal
    v-model:show="modelValue.toDelete"
    :mask-closable="false"
    preset="dialog"
    title="确认"
    content="你确认"
    positive-text="确认"
    negative-text="算了"
    @positive-click="onPositiveClick"
    @negative-click="modelValue.toDelete = false"
  />
</template>

<script lang="ts" setup generic="T extends object & { toEdit: boolean,toDelete:boolean,toDealRowId: number|undefined}">
import { TaskLogData } from "@model/TaskLogMap";
import type { TimeManageList } from "@model/timeManage";
import UseCacheIdStatMap from "@pinias/UseCacheIdStatMap";
import UseCacheMemory from "@pinias/UseCacheMemory";
import UseCacheTaskLog from "@pinias/UseCacheTaskLog";
import UseLifeModel from "@pinias/UseLifeModel";
import UseSurfaceSetting from "@pinias/UseSurfaceSetting";
import UseTimeManageList from "@pinias/UseTimeManageList";
import pinia from "@pinias/pinia";
import { NModal } from "naive-ui";
import { computed } from "vue";

// 全局数据
const { getLifeModel } = UseLifeModel(pinia);
const lifeModel = getLifeModel();
const { getSurfaceSetting } = UseSurfaceSetting(pinia);
const surfaceSetting = getSurfaceSetting();
const { getTimeManageList } = UseTimeManageList(pinia);
const timeManageList: TimeManageList = getTimeManageList();
const { getCacheIdStatMap } = UseCacheIdStatMap(pinia);
const cacheIdStatMap = getCacheIdStatMap();
const { getCacheTaskLogMap } = UseCacheTaskLog(pinia);
const cacheTaskLogMap = getCacheTaskLogMap();
const { getCacheMemory } = UseCacheMemory(pinia);
const cacheMemory = getCacheMemory();
// 父组件数据
const props = defineProps<{ modelValue: T }>();
const emit = defineEmits(["update:modelValue"]);
const modelValue = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
const onPositiveClick = async () => {
  //message.success("Submit");
  const id = modelValue.value.toDealRowId;
  if (id !== undefined) {
    const stat = await cacheIdStatMap.getIdStatNotCreate(id);
    const oldFatherId = await TaskLogData.getFatherId(2, id);
    if (stat !== undefined) {
      const toDelTaskLog = new Array<TaskLogData>();
      for (const statElement of stat.everyDayLogMap.values()) {
        for (const logKey of statElement) {
          const taskLog = await cacheTaskLogMap.getTaskLogData(logKey);
          if (taskLog !== undefined) toDelTaskLog.push(taskLog);
        }
      }
      if (toDelTaskLog.length > 0) {
        // 统计情况：
        await cacheIdStatMap.changeTaskLogData4Stat(
          toDelTaskLog,
          oldFatherId.map(value => value.id),
          [],
          [],
        );
        // 回忆情况
        await cacheMemory.deleteMemory(toDelTaskLog);
        // 日志情况
        await cacheTaskLogMap.deleteTaskLogData(toDelTaskLog.map(value => value.id));
      }
    }
    lifeModel.deleteTimeManageById(surfaceSetting.showLifeModel, id);
    timeManageList.deleteTimeManageById(id);
  }

  // tableData.value = tableData.value.filter(item => item.id !== id);
  modelValue.value.toDelete = false;
  surfaceSetting.LifeModelItemsKey = new Date().getTime();
};
</script>
